package com.aspose.cells;

import java.security.PublicKey;
import java.security.Signature;
import java.util.HashMap;
import java.util.Iterator;
import javax.security.cert.X509Certificate;

class zrj {
   private DigitalSignatureCollection a = new DigitalSignatureCollection();

   public DigitalSignatureCollection a() {
      return this.a;
   }

   public zrj(com.aspose.cells.a.f.zi var1) throws Exception {
      this.a(var1);
   }

   private void a(zcfl var1, String[] var2) {
      Iterator var3 = var1.j().iterator();

      while(var3.hasNext()) {
         zcfl var4 = (zcfl)var3.next();
         zcfl var5 = (zcfl)var1.k();
         Iterator var6 = ((zcfl)var4.k()).j().iterator();

         while(var6.hasNext()) {
            zcfl var7 = (zcfl)var6.next();
            if ("SignatureComments".equals(var7.i())) {
               var2[0] = var7.d();
               return;
            }
         }
      }

      var2[0] = "";
   }

   private void a(zcfl var1, DateTime[] var2) {
      var2[0] = DateTime.a;
      String var3 = "";
      String var4 = "";
      zcfs var5 = var1.j();

      for(int var7 = 0; var7 < var5.a(); ++var7) {
         zcfl var6 = (zcfl)var5.a(var7);
         if ("idSignatureTime".equals(zarv.a(var6, "Id"))) {
            zcfl var8 = (zcfl)var6.k();

            for(int var9 = 0; var9 < var8.j().a(); ++var9) {
               zcfl var10 = (zcfl)var8.j().a(var9);
               if ("mdssi:Format".equals(var10.i())) {
                  var3 = var10.d();
               }

               if ("mdssi:Value".equals(var10.i())) {
                  var4 = var10.d();
               }
            }

            try {
               var2[0] = DateTime.a(var4).toUniversalTime();
            } catch (Exception var11) {
               ;
            }
         }
      }

   }

   private boolean a(String var1, com.aspose.cells.a.f.zi var2, String[] var3, DateTime[] var4) throws Exception {
      zcfk var5 = new zcfk();
      var5.a(var1, false);
      zcfl var6 = var5.f();
      zcfl var7 = (zcfl)var6.g("SignedInfo");
      if (var7 == null) {
         throw new Exception("Cannot find Signature element");
      } else {
         String var8 = "";
         HashMap var9 = zarv.a;
         synchronized(zarv.a) {
            zarv.a();
            String[] var10 = new String[]{var8};
            zarv.a(var7, var10, "http://www.w3.org/2000/09/xmldsig#");
            var8 = var10[0];
         }

         var3[0] = null;
         var4[0] = DateTime.a;
         zcfs var25 = var6.e("Object");
         zcfs var26 = var7.e("Reference");
         Iterator var11 = var26.iterator();

         label96:
         while(var11.hasNext()) {
            zcfl var12 = (zcfl)var11.next();
            Iterator var13 = var25.iterator();

            label94:
            while(true) {
               zcfl var14;
               do {
                  if (!var13.hasNext()) {
                     continue label96;
                  }

                  var14 = (zcfl)var13.next();
               } while(!var12.b("URI").substring(1).equals(var14.b("Id")));

               var8 = "";
               HashMap var15 = zarv.a;
               synchronized(zarv.a) {
                  zarv.a();
                  String[] var16 = new String[]{var8};
                  zarv.a(var14.c(), var16);
                  var8 = var16[0];
               }

               byte[] var27 = zbbm.b(com.aspose.cells.b.a.g.za.getASCII().a(var8));
               byte[] var28 = com.aspose.cells.b.a.ze.b(var12.g("DigestValue").d());
               if (!this.a(var27, var28)) {
                  return false;
               }

               Iterator var17 = var14.j().iterator();

               while(true) {
                  while(true) {
                     if (!var17.hasNext()) {
                        continue label94;
                     }

                     zcfl var18 = (zcfl)var17.next();
                     if ("SignatureProperties".equals(var18.i())) {
                        if (zarv.a((zcfl)var18.g("SignatureProperty"), "Id").startsWith("idOfficeV1Details")) {
                           this.a(var18, var3);
                        }

                        if (zarv.a((zcfl)var18.g("SignatureProperty"), "Id").startsWith("idSignatureTime")) {
                           this.a(var18, var4);
                        }
                     } else if ("Manifest".equals(var18.i()) && var18.j().a() > 0) {
                        Iterator var19 = var18.j().iterator();

                        while(var19.hasNext()) {
                           zcfl var20 = (zcfl)var19.next();
                           if ("Reference".equals(var20.i()) && !zarv.a(var20, "URI").endsWith(".relationships+xml")) {
                              try {
                                 byte[] var21 = this.a(var2, var20.b("URI"));
                                 if (!this.a(zbbm.b(var21), com.aspose.cells.b.a.ze.b(var20.g("DigestValue").d()))) {
                                    return false;
                                 }
                              } catch (Exception var22) {
                                 ;
                              }
                           }
                        }
                     }
                  }
               }
            }
         }

         return true;
      }
   }

   private boolean a(byte[] var1, byte[] var2) {
      if (var1.length != var2.length) {
         return false;
      } else {
         for(int var3 = 0; var3 < var1.length; ++var3) {
            if (var1[var3] != var2[var3]) {
               return false;
            }
         }

         return true;
      }
   }

   private boolean a(String var1, X509Certificate[] var2) {
      try {
         zcfk var3 = new zcfk();
         var3.a(var1, false);
         zcfl var4 = var3.f();
         zcfl var5 = (zcfl)var4.g("KeyInfo");
         zcfl var6 = (zcfl)var5.g("X509Data");
         zcfl var7 = (zcfl)var6.g("X509Certificate");
         byte[] var8 = com.aspose.cells.b.a.ze.b(var7.d());
         var2[0] = X509Certificate.getInstance(var8);
         zcfl var9 = (zcfl)var4.g("SignedInfo");
         String var10 = zarv.a(var9);
         byte[] var11 = com.aspose.cells.b.a.g.za.getASCII().a(var10);
         PublicKey var12 = var2[0].getPublicKey();
         Signature var13 = Signature.getInstance("SHA1withRSA");
         var13.initVerify(var12);
         var13.update(var11);
         return var13.verify(com.aspose.cells.b.a.ze.b(var4.g("SignatureValue").d()));
      } catch (Exception var14) {
         return false;
      }
   }

   private boolean a(com.aspose.cells.a.f.zi var1) throws Exception {
      com.aspose.cells.a.f.zh var2 = var1.a("[Content_Types].xml");
      com.aspose.cells.b.a.d.zm var3 = var1.a(var2);
      com.aspose.cells.b.a.d.zn var4 = new com.aspose.cells.b.a.d.zn(var3);
      String var5 = var4.e();
      zcfk var6 = new zcfk();
      var6.a(var5, false);
      zcfl var7 = var6.f();
      if (var7 != null) {
         for(int var8 = 0; var8 < var7.j().a(); ++var8) {
            zcfl var9 = (zcfl)var7.j().a(var8);
            if (zarv.a(var9, "ContentType").endsWith("digital-signature-xmlsignature+xml")) {
               X509Certificate var10 = null;
               String var11 = null;
               DateTime var12 = null;
               boolean var13 = true;
               String var14 = this.b(var1, zarv.a(var9, "PartName"));
               X509Certificate[] var15 = new X509Certificate[]{var10};
               boolean var16 = !this.a(var14, var15);
               var10 = var15[0];
               if (var16) {
                  var13 = false;
               }

               String[] var17 = new String[]{var11};
               DateTime[] var18 = new DateTime[]{var12};
               boolean var19 = !this.a(var14, var1, var17, var18);
               var11 = var17[0];
               var12 = var18[0];
               if (var19) {
                  var13 = false;
               }

               DigitalSignature var20 = new DigitalSignature(var10, var11, var12, var13);
               com.aspose.cells.b.a.a.ze.a(this.a.a, (Object)var20);
            }
         }
      }

      return true;
   }

   private byte[] a(com.aspose.cells.a.f.zi var1, String var2) throws Exception {
      int var3 = var2.charAt(0) == '/' ? 1 : 0;
      int var4 = var2.indexOf(63);
      var4 = var4 == -1 ? var2.length() - var3 : var4 - var3;
      com.aspose.cells.a.f.zh var5 = var1.a(var2.substring(var3, var3 + var4));
      com.aspose.cells.b.a.d.zm var6 = var1.a(var5);
      byte[] var7 = new byte[(int)var5.a()];
      var6.a(var7, 0, var7.length);
      return var7;
   }

   private String b(com.aspose.cells.a.f.zi var1, String var2) throws Exception {
      int var3 = var2.charAt(0) == '/' ? 1 : 0;
      int var4 = var2.indexOf(63);
      var4 = var4 == -1 ? var2.length() - var3 : var4 - var3;
      com.aspose.cells.a.f.zh var5 = var1.a(var2.substring(var3, var3 + var4));
      com.aspose.cells.b.a.d.zn var6 = new com.aspose.cells.b.a.d.zn(var1.a(var5));
      return var6.e();
   }
}
